package leetcode;

import java.util.HashMap;
import java.util.Map;
//13题(罗马数字转整数)
public class ques_13 {
    public static int romanToInt(String s) {
        Map<Character,Integer> valueChar=new HashMap<>();
        valueChar.put('I',1);
        valueChar.put('V',5);
        valueChar.put('X',10);
        valueChar.put('L',50);
        valueChar.put('C',100);
        valueChar.put('D',500);
        valueChar.put('M',1000);
        int target=0;
        for (int i = 0; i < s.length(); i++) {
            char cur,next;
            if(i!=s.length()-1){
                cur=s.charAt(i);
                next=s.charAt(i+1);
                //判断特殊情况
                if(valueChar.get(cur)<valueChar.get(next)){
                    target+=valueChar.get(next)-valueChar.get(cur);
                    i+=1;//注意不是直接＋2，在continue后循环会自动再加上1
                    continue;
                }
                target+=valueChar.get(cur);
            }else {
                target+=valueChar.get(s.charAt(i));//接收最后一个罗马数字(不是特殊情况结尾的情况下)

            }
        }
        return target;
    }

    public static void main(String[] args) {
        int sum=romanToInt("MCMXCIV");
        System.out.println(sum);
    }

}
